package com.huluobo.a2203parkdemo.repair

import android.app.DatePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.DatePicker
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2203parkdemo.R
import com.huluobo.a2203parkdemo.databinding.ActivityAddRepairBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.Calendar

@AndroidEntryPoint
class AddRepairActivity : BaseActivity<ActivityAddRepairBinding, RepairViewModel>() {
    private var repairData = ""
    private var repairDesc = ""
    override fun initData() {
        viewModel.addRepairSuccess.observe(this) {
            ToastUtils.showLong("添加维修成功")
            finish()
        }
    }

    override fun initView() {
        binding.addRepairTimeIv.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                object : DatePickerDialog.OnDateSetListener {
                    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                        repairData = "$year-$month-$dayOfMonth"
                        binding.addRepairTimeNowTv.text = "$year-${month+1}-$dayOfMonth"
                    }

                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }

        binding.addRepairCommitBtn.setOnClickListener {
            repairDesc = binding.addRepairDescEt.text.toString()
            if (repairData.isNotEmpty() && repairData.isNotEmpty()) {
                val map = mutableMapOf<String, String>()
                map[Const.PARAM_REPAIR_DATA] = repairData
                map[Const.PARAM_REPAIR_DESC] = repairDesc
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                viewModel.addRepair(body)
            } else {
                ToastUtils.showLong("请选择时间或者填入维修描述")
            }
        }
    }
}